package com.example.meituan.activity.user;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import com.bumptech.glide.util.ContentLengthInputStream;
import com.example.meituan.R;
import com.example.meituan.activity.bean.CommentBean;
import com.example.meituan.activity.bean.ShopBean;
import com.example.meituan.activity.bean.ShopCarBean;
import com.example.meituan.activity.bean.TypeBean;
import com.example.meituan.activity.user.adapter.CommentAdapter;
import com.example.meituan.activity.user.adapter.ShopCarAdapter;
import com.example.meituan.dao.CommentDao;
import com.example.meituan.dao.ShopCarDao;

import java.util.List;

public class CommentSheet extends DialogFragment {
    private ImageView exit;
    private ShopBean shopBean;
    CommentAdapter adapter;
    androidx.recyclerview.widget.RecyclerView recyclerView;

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState){
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View view = LayoutInflater.from(getContext()).inflate(R.layout.comment_sheet_layout, null);
        exit=view.findViewById(R.id.exit_comment);
        recyclerView=view.findViewById(R.id.comment_recyclerView);
        if (shopBean != null) {
            initializeViews();
        }

        setClickListeners(view);

        return builder.setView(view).create();
    }

    private void initializeViews(){
        List<CommentBean> commentBeans= CommentDao.getAllCommentsByShopId(shopBean.getId());
        if (commentBeans == null || commentBeans.isEmpty()) {
            Toast.makeText(getActivity(), "没有数据显示", Toast.LENGTH_SHORT).show();
            recyclerView.setAdapter(null);
        } else {
            adapter = new CommentAdapter(commentBeans); ;
            recyclerView.setAdapter(adapter);
        }

    }

    private void setClickListeners(View view){
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void show(ShopBean shopBean,  androidx.fragment.app.FragmentManager fragmentManager) {
        this.shopBean=shopBean;
        super.show(fragmentManager, "product_details");
    }
}
